import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
var store = new Vuex.Store({
    state:{
        car:JSON.parse(localStorage.getItem('car')) || []
    },
    mutations:{
        getCarcount(state,infos){
            var flag = false;
            state.car.some(item=>{
                if(item.id==infos.id){
                    item.count+=infos.count;
                    flag=true;
                    return;
                }
            })
            if(!flag){
                state.car.push(infos)
            }
            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },
    getters:{
       getAllcount(state){
           var c = 0;
            state.car.forEach(item=>{
                c+=item.count
            })
            return c
       }
    }
})
export default store